package daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dao.CollegeDao;
import pojo.Book;
import pojo.BookImage;
import pojo.College;
import pojo.CollegeBrochure;
import utils.DruidUtils;

public class CollegeDaoImpl implements CollegeDao{

	@Override
	public ArrayList<College> getcolleges() {
		Connection conn = DruidUtils.getConnection();
		String sql = "select * from college";
		ArrayList<College> cList = new ArrayList<College>();
		try {
			PreparedStatement pre = conn.prepareStatement(sql);
			
			ResultSet res = pre.executeQuery();
			while(res.next()) {
				College c = new College();
				c.setCollegeid(res.getInt("collegeid"));
				c.setCollegeName(res.getString("collegeName"));
				c.setCollegeProvince(res.getInt("collegeProvince"));
				c.setCollegeLogo(res.getString("collegeLogo"));
				cList.add(c);
			}
			res.close();
			pre.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cList;
	}

	@Override
	public ArrayList<CollegeBrochure> getcollegeBrochure(int collegeid) {
		Connection conn = DruidUtils.getConnection();
		String sql = "select * from collegebrochure where collegeid=?";
		ArrayList<CollegeBrochure> cbList = new ArrayList<CollegeBrochure>();
		try {
			PreparedStatement pre = conn.prepareStatement(sql);
			pre.setInt(1, collegeid);
			
			ResultSet res = pre.executeQuery();
			while(res.next()) {
				CollegeBrochure cb = new CollegeBrochure();
				cb.setCbid(res.getInt("cbid"));
				cb.setCollegeid(res.getInt("collegeid"));
				cb.setCbContent(res.getString("cbContent"));
				cb.setCbTitle(res.getString("cbTitle"));
				cb.setCbReadNum(res.getInt("cbReadNum"));
				cbList.add(cb);
			}
			res.close();
			pre.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cbList;
	}
	
}
